using System;
using System.Collections.Generic;
using System.Text;
using Deepcode.Flux.Core.Services.Configuration;

// Disable warning 618 - concerns System.Web.Mail being obsolete
#pragma warning disable 618
namespace Deepcode.Flux.Core.Services.MailHandlers
{
	/// <summary>
	/// Alternative mail handler class - this one uses legacy .NET 1.1 
	/// SMTP headers to implement SMTP-Auth. Some mail servers won't work
	/// with .NET 2.0's Network credentials, so instead this class uses
	/// the legacy System.Web.Mail classes to send mail with optional
	/// SMTP-Auth headers
	/// </summary>
	public class LegacySmtpAuthMail : IMailHandler
	{
		public void SendMail(StandardMailMessage mm)
		{
			System.Web.Mail.MailMessage m = new System.Web.Mail.MailMessage();
			m.From = mm.From;
			m.To = mm.To;
			m.Cc = mm.CC;
			m.Bcc = mm.BCC;
			m.Subject = mm.Subject;
			m.Body = mm.Body;
			if( mm.BodyIsHtml )
				m.BodyFormat = System.Web.Mail.MailFormat.Html;
			else
				m.BodyFormat = System.Web.Mail.MailFormat.Text;

			if (FluxConfig.MailUsername != "")
			{
				// Add the SMTP Authentication Data
				m.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 2);			// Enable TCP SMTP (Required to use SMTP AUTH)
				m.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);		// Enable SMTP AUTH
				m.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", FluxConfig.MailUsername);	// Send Username
				m.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", FluxConfig.MailPassword);	// Send Password
				m.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", FluxConfig.MailServer);	// Set the server
				m.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);		// Set the port
			}
			
			System.Web.Mail.SmtpMail.SmtpServer = FluxConfig.MailServer;
			System.Web.Mail.SmtpMail.Send( m );
		}
	}
}
#pragma warning restore 618